// pages/addAddress/addAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "请填写地址",
    name: "",
    phone: "",
    addName: "",
    addrId: ""
  },
  map: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function (res) {
        wx.chooseLocation({
          success: function (res) {
            console.log(res)
            console.log(res.address);

            that.setData({
              address: res.address,
              addName: res.name
            });
            if (that.data.address == that.data.addName) {
              that.setData({
                address: that.data.address
              })
            } else {
              that.setData({
                address: that.data.address + "(" + that.data.addName + ")"
              })
            }

            wx.setStorageSync("location", res)
          },
        })
      },
      fail: function (res) {
        wx.getSetting({
          success: function (res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        console.log(data);
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          //授权成功之后，再调用chooseLocation选择地方
                          wx.chooseLocation({
                            success: function (res) {
                              console.log(res);
                              that.setData({
                                address: res.address,
                                addName: res.name
                              });
                              if (that.data.address == that.data.addName) {
                                that.setData({
                                  address: that.data.address
                                })
                              } else {
                                that.setData({
                                  address: that.data.address + "(" + that.data.addName + ")"
                                })
                              }

                              wx.setStorageSync("location", res)
                            },
                          })
                        } else {
                          wx.showToast({
                            title: '授权失败',
                            icon: 'success',
                            duration: 1000
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '调用授权窗口失败',
              icon: 'success',
              duration: 1000
            })
          }
        })

      }
    })

  },
  // 获取用户名并储存在本地
  username: function (e) {
    var that = this;
    // if(wx.getStorageSync("mes")!=""){
    //   console.log("储存名字")
    //   that.setData({
    //     name:wx.getStorageSync("mes").name
    //   })
    // }else{
    that.setData({
      name: e.detail.value
    })
    // }

    console.log(this.data.name)
  },
  phone: function (e) {


    this.setData({
      phone: e.detail.value
    })
    console.log(this.data.phone)


  },
  save: function () {
    var that = this;
    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(that.data.name))) {

      wx.showToast({

        title: '姓名有误',

        duration: 2000,

        icon: "none"

      })
    } else if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(that.data.phone))) {

      wx.showToast({

        title: '手机号码有误',

        duration: 2000,

        icon: "none"

      })
    } else if (that.data.address == "请填写地址") {
      wx.showToast({

        title: '请填写地址',

        duration: 2000,

        icon: "none"
      })
    } else {
      wx.request({
        url: 'https://www.xiaozheng8.com/sendUserAddr/add',
        method: "post",
        data: {
          name: this.data.name,
          phone: this.data.phone,
          addr: this.data.address,
          userId: wx.getStorageSync("userId"),
          lat: wx.getStorageSync("location").latitude,
          lng: wx.getStorageSync("location").longitude,
        },
        success: function (res) {
          console.log(res);

          console.log(wx.getStorageSync("location").longitude)
        }

      });
      if (that.data.addrId == null || that.data.addrId == "" || that.data.addrId == undefined) {

      } else {
        wx.request({
          url: 'https://www.xiaozheng8.com/sendUserAddr/delete',
          data: {
            addrId: that.data.addrId
          },
          method: 'post',
          success: function (res) {
            console.log(res);
            console.log("删除地址")
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
      wx.navigateBack({

      })
      // wx.redirectTo({
      //   url: '../addressAdministration/addressAdministration?add='+2,
      // })

    }


    // var name=[];
    // var phone=[];
    // var addr=[];
    // var Arr=new Array();
    // Arr="name,phone,addr";
    // name.push(this.data.name);
    // phone.push(this.data.phone);
    // addr.push(this.data.address);
    // for(var i=0;i<Arr.length;i++){
    //   Arr = Arr + "\"" + Arr[i] + "\"" + ":" + "[" + "]" + ",";
    // };

    // wx.setStorageSync("name", name);
    // wx.setStorageSync("phone",phone);
    // wx.setStorageSync("addr",addr);


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);

    if (options.addrId == "" || options.addrId == null || options.addrId == undefined) {

    } else {
      that.setData({
        addrId: options.addrId
      })
      wx.request({
        url: 'https://www.xiaozheng8.com/sendUserAddr/findAll',
        method: "post",
        data: {
          userId: wx.getStorageSync("userId"),
        },
        success: function (res) {
          console.log(res.data.data);
          console.log(res.data.data.length);
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].addrId == options.addrId) {
              that.setData({
                address: res.data.data[i].addr,
                name: res.data.data[i].name,
                phone: res.data.data[i].phone,
              })
              console.log(that.data.address)
            }
          }
        }
      })





    }

    // var name = wx.getStorageSync("name");
    // var phone = wx.getStorageSync("phone");
    // var addr = wx.getStorageSync("addr");
    // this.setData({
    //   name: name,
    //   phone: phone,
    //   addr: addr
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})